﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Control;
using Enterprise.Model.Vote;
using Enterprise.Service.Vote;
namespace Enterprise.UI.Web.Modules.Vote
{
    public partial class Vote :Enterprise.Service.BasePage
    {
        string vId = (string)Utility.sink("vId", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {               
                OnStart();
            }
        }

        private void OnStart()
        {
            VoteService vService = new VoteService();
            var q = vService.VoteDisp(vId);
            if (q != null)
            {
                lb_Title.Text = q.VTITLE;
                lb_EndTime.Text = q.VENDTIME.ToShortDateString();
                lb_Remark.Text = q.VREMARK;
                lb_CUser.Text = Enterprise.Service.User.UserService.GetUserName(q.VCREATEUSER);
                lb_CTime.Text = q.VCREATETIME.ToString();

                VoteItemService viService = new VoteItemService();
                var list = viService.VoteItemList(vId).OrderBy(p => p.VITEMCODE);
                GridView1.DataSource = list;
                GridView1.DataBind();

                OnCommand(Convert.ToDateTime(q.VENDTIME.ToShortDateString()));
            }
        }

        private void OnCommand(DateTime _endtime)
        {
            if (Cmd == "")
            {
                VoteResultService vrService = new VoteResultService();
                var query = vrService.VoteResultList().Where(p => p.VID == vId).FirstOrDefault();
                if (query == null&&_endtime>Convert.ToDateTime( DateTime.Now.ToShortDateString()))
                {
                    CreateBT.AddButton("a.gif", "投票", "Vote.aspx?vId=" + vId + "&Cmd=Vote", Utility.PopedomType.A, HeadMenu1);
                }
                else
                {
                    CreateBT.AddButton("Colume.png", "查看结果", "Result.aspx?vId=" + vId, Utility.PopedomType.List, HeadMenu1);
                }
                CreateBT.AddButton("back.gif", "返回", "Index.aspx", Utility.PopedomType.List, HeadMenu1);

                SubmitDiv.Visible = false;
            }
            else if (Cmd == "Vote")
            {
                CreateBT.AddButton("back.gif", "返回", "Vote.aspx?vId="+vId, Utility.PopedomType.List, HeadMenu1);
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (Cmd != "Vote")
            {
                e.Row.Cells[0].Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                VoteResultService vrService = new VoteResultService();
                VoteResultEntity entity = new VoteResultEntity();
                entity.DB_Option_Action = "Insert";
                entity.VID = vId;
                entity.VRESULTUSERID = Utility.Get_UserId;
                entity.VRESULTTIME = DateTime.Now;
                string selectedValue = Request.Form["myradiobutton"];
                if (!string.IsNullOrEmpty(selectedValue))
                {
                    entity.VITEMID = Convert.ToInt32(selectedValue);
                    entity.VITEMCODE = VoteItemService.GetVoteItemCode(entity.VID, entity.VITEMID);
                    if (!string.IsNullOrEmpty(entity.VITEMCODE))
                    {
                        vrService.VoteResultExecute(entity);
                    }
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Vote.aspx?vId=" + vId);
        }

    }
}